1 package edu.jiangxin.apktoolbox.swing.extend.listener; 2 3 import edu.jiangxin.apktoolbox.utils.FileUtils; 4 5 import javax.swing.*; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.io.File; 9 10 public class SelectDirectoryListener implements ActionListener { 11 private final String dialogTitle; 12 private final JTextField pathTextField; 13 14 public SelectDirectoryListener(String dialogTitle, JTextField pathTextField) { 15 super(); 16 this.dialogTitle = dialogTitle; 17 this.pathTextField = pathTextField; 18 } 19 20 @Override 21 public void actionPerformed(ActionEvent e) { 22 JFileChooser jfc = new JFileChooser(); 23 jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 24 jfc.setDialogTitle(dialogTitle); 25 int ret = jfc.showDialog(new JLabel(), null); 26 if (ret == JFileChooser.APPROVE_OPTION) { 27 File file = jfc.getSelectedFile(); 28 String path = FileUtils.getCanonicalPathQuiet(file); 29 pathTextField.setText(path); 30 } 31 } 32 }